package com.ruge.user.service.impl;

import com.ruge.core.error.BaseError;
import com.ruge.core.exception.BaseBusinessException;
import com.ruge.core.response.Response;
import com.ruge.core.tool.AssertTool;
import com.ruge.tool.digest.DigestTool;
import com.ruge.tool.json.JsonTool;
import com.ruge.tool.str.StringTool;
import com.ruge.user.dao.UserRepository;
import com.ruge.user.domain.model.User;
import com.ruge.user.domain.request.ListUserRequest;
import com.ruge.user.domain.request.SaveUserRequest;
import com.ruge.user.error.UserError;
import com.ruge.user.service.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ruge.wu
 * @since 2021/12/24 13:29
 */
@Service
public class UserServiceImpl implements UserService, UserDetailsService {

    @Resource
    private UserRepository userRepository;

    /**
     * 用户数据保存
     *
     * @param request {@link SaveUserRequest}
     * @return {@link Response}
     */
    @Override
    public Response save(SaveUserRequest request) {
        User objToBean = JsonTool.getObjToBean(request, User.class);
        for (int i = 0; i < 100; i++) {
            User user = User.builder()
                    .userName("张" + i)
                    .password("密码" + i)
                    .build();
            userRepository.save(user);
        }
        return Response.create();
    }

    /**
     * 用户数据查询
     *
     * @param request {@link ListUserRequest}
     * @return {@link Response}
     * @Override public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
     * Predicate p1 = criteriaBuilder.equal(root.get("id"),"2");
     * Predicate p2 = criteriaBuilder.equal(root.get("id"),"3");
     * return criteriaBuilder.or(p1,p2);
     * }
     */
    @Override
    public Response list(ListUserRequest request) {
        return Response.create(userRepository.findAll(request.getPageable()));
    }

    /**
     * 校验手机号是否已注册
     *
     * @param mobile 手机号
     * @return {@link Response}
     */
    @Override
    public Response checkPhoneIsRegister(String mobile) {
        User user = userRepository.findFirstByMobile(mobile).orElseThrow(() -> new BaseBusinessException(UserError.MOBILE_HAS_REGISTER));
        AssertTool.mustTrue(user.getIsValid(), BaseError.DATA_HAS_LOCKED);
        return Response.create();
    }

    @Override
    public Response getUserInfo(List<Long> aid) {
        List<User> list = userRepository.findAllById(aid);
        return Response.create(list);
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userRepository.findFirstByMobile(s).orElse(new User());
        if (StringTool.isBlank(user.getId())) {
            user.setMobile(s);
            user.setPassword(DigestTool.md5Hex("123456"));
            user.setIsValid(Boolean.TRUE);
            userRepository.save(user);
        }

        // 初始化登陆认证对象
//        SignInIdentity signInIdentity = JsonTool.getObjToBean(user, SignInIdentity.class);
        return null;
    }
}
